/**
  ******************************************************************************
  * @file    GPIO/GPIO_IOToggle/stm32f4xx_it.c 
  * @author  MCD Application Team
  * @version V1.4.0
  * @date    04-August-2014
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_it.h"
#include <string.h>
#include <time.h>

#include "drv_SPI_Nixie.h"
#include "drv_Servo.h"
#include "drv_USART.h"
#include "drv_ASRPRO.h"
#include "drv_WIFI_TCP.h"
#include "drv_IIC_SHT20.h"

#include "drivers_beep.h"
#include "drv_Fan.h"
#include "drv_Motor.h"
#include "drivers_LED.h"
/** @addtogroup STM32F4xx_StdPeriph_Examples
  * @{
  */

/** @addtogroup GPIO_IOToggle
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}

/******************************************************************************/
/*                 STM32F4xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f40xx.s/startup_stm32f427x.s/startup_stm32f429x.s).    */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

//外部中断
void EXTI9_5_IRQHandler(void)
{
  if (EXTI_GetITStatus(EXTI_Line9))
  {
    EXTI_ClearITPendingBit(EXTI_Line9);

  }
}

extern SHT20_val tem_hum_data;
extern uint8_t tim7_flg;

//定时器中断控制温度过高
void TIM7_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM7 , TIM_IT_Update))
  {
    TIM_ClearITPendingBit(TIM7 , TIM_IT_Update);
    if (tim7_flg == 1)
    {
      tim7_flg =0;
      if (tem_hum_data.tem > 32)
      {
        USART_Send_str(USART2 , "High Temp" , sizeof("High Temp"));
      }
    }
  }
}

//语音模块中断控制
extern char USART2_Recv_Buff[255];
extern uint8_t USART2_Recv_Index;
extern uint8_t Current_Sta;
extern uint8_t Current_Exp;
void USART2_IRQHandler(void)
{
  if (USART_GetITStatus(USART2 , USART_IT_RXNE))
  {
    char receivedChar = (char)USART_ReceiveData(USART2);
    USART_ClearITPendingBit(USART2 , USART_IT_RXNE);      //清空发送完成标志
    USART_SendData(USART6 , receivedChar);
    USART2_Recv_Buff[USART2_Recv_Index++] = receivedChar;
    if (receivedChar == '\n' || receivedChar == '\r')
    {
        USART2_Recv_Buff[USART2_Recv_Index] = '\0';
        if (strstr(USART2_Recv_Buff , "LED open") != NULL)
        {
          Current_Sta = LED_Open;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "LED close") != NULL)
        {
          Current_Sta = LED_CLose;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Fan open") != NULL)
        {
          Current_Sta = Fan_Open;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Fan close") != NULL)
        {
          Current_Sta = Fan_Close;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Stand") != NULL)
        {
          Current_Sta = DOG_Stand;
          Current_Exp = Laugh;
          Fan_OFF();
          beep_off();
          Motor_OFF();
          LED_OFF(LED_ID_1);
          LED_OFF(LED_ID_2);
          LED_OFF(LED_ID_3);
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Sit") != NULL)
        {
          Current_Sta = DOG_Sit;
          Current_Exp = Smile;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Handshake") != NULL)
        {
          Current_Sta = DOG_Handshake;
          Current_Exp = Shy;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Move forward") != NULL)
        {
          Current_Sta = DOG_Move_forward;
          Current_Exp = Work_hard;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Move back") != NULL)
        {
          Current_Sta = DOG_Move_back;
          Current_Exp = Work_hard;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Turn left") != NULL)
        {
          Current_Sta = DOG_Turn_left;
          Current_Exp = Work_hard;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Turn right") != NULL)
        {
          Current_Sta = DOG_Turn_right;
          Current_Exp = Work_hard;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Dance") != NULL)
        {
          Current_Sta = DOG_Dance;
          Current_Exp = Shy;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Lie down") != NULL)
        {
          Current_Sta = DOG_Lie_down;
          Current_Exp = Smile;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Push up") != NULL)
        {
          Current_Sta = DOG_Push_up;
          Current_Exp = Work_hard;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "On fire") != NULL)
        {
          beep_on();
          Fan_ON();
          Current_Exp = Cry;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "go home") != NULL)
        {
          LED_ON(LED_ID_1);
          LED_ON(LED_ID_2);
          LED_ON(LED_ID_3);
          Current_Exp = Smile;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Show data") != NULL)
        {
          Current_Exp = Temp_Hum_Data;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "Show time") != NULL)
        {
          Current_Exp = Time;
          clear_USART2_Recv_Buff();
        }
        else if (strstr(USART2_Recv_Buff , "High Temp") != NULL)
        {
          Motor_ON();
          Fan_ON();
          Current_Exp = Afraid;
          clear_USART2_Recv_Buff();
        }
        clear_USART2_Recv_Buff();
    }
  }
}

//WIFI模块中断控制
extern char USART3_Recv_Buff[USART3_RECV_BUFF_SIZE];
extern uint16_t USART3_Recv_Index;
extern uint16_t USART3_Recv_Flag;
void USART3_IRQHandler(void)
{
  uint16_t receive_data = 0;
  if (USART_GetITStatus(USART3 , USART_IT_RXNE))
  {
    USART_ClearITPendingBit(USART3 , USART_IT_RXNE);
    receive_data = USART_ReceiveData(USART3);
	  USART_SendData(USART6, receive_data);
    if (USART3_Recv_Index < USART3_RECV_BUFF_SIZE)
    {
        USART3_Recv_Buff[USART3_Recv_Index++] = receive_data;
        USART3_Recv_Buff[USART3_Recv_Index] = '\0';
        if (strstr(USART3_Recv_Buff , "OK") != NULL)
        {
          USART3_Recv_Flag = 1;
          clear_USART3_Recv_Buff();
          printf("flag:%d\r\n" , USART3_Recv_Flag);
        }
        else if (strstr(USART3_Recv_Buff , "FALL") != NULL || strstr(USART3_Recv_Buff , "ERROR") != NULL)
        {
          USART3_Recv_Flag = 2;
          clear_USART3_Recv_Buff();
         // printf("error\r\n");
        }
        else if (strstr(USART3_Recv_Buff , "Openled") != NULL)
        {
          Current_Sta = LED_Open;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Closeled") != NULL)
        {
          Current_Sta = LED_CLose;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Stop") != NULL)
        {
          Current_Sta = DOG_Stand;
          Current_Exp = Laugh;
          Fan_OFF();
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Sit") != NULL)
        {
          Current_Sta = DOG_Sit;
          Current_Exp = Smile;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Handshake") != NULL)
        {
          Current_Sta = DOG_Handshake;
          Current_Exp = Shy;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Move") != NULL)
        {
          Current_Sta = DOG_Move_forward;
          Current_Exp = Work_hard;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Back") != NULL)
        {
          Current_Sta = DOG_Move_back;
          Current_Exp = Work_hard;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Left") != NULL)
        {
          Current_Sta = DOG_Turn_left;
          Current_Exp = Work_hard;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Right") != NULL)
        {
          Current_Sta = DOG_Turn_right;
          Current_Exp = Work_hard;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Dance") != NULL)
        {
          Current_Sta = DOG_Dance;
          Current_Exp = Shy;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Lie") != NULL)
        {
          Current_Sta = DOG_Lie_down;
          Current_Exp = Smile;
          clear_USART3_Recv_Buff();
        }
        else if (strstr(USART3_Recv_Buff , "Swim") != NULL)
        {
          Current_Sta = DOG_Push_up;
          Current_Exp = Work_hard;
          clear_USART3_Recv_Buff();
        }
        if (USART3_Recv_Index >= USART3_RECV_BUFF_SIZE - 1)
        {
          clear_USART3_Recv_Buff();
        }
    }
    else
    {
      clear_USART3_Recv_Buff();
    }
  }
}
  


//火焰传感器中断  模拟着火
void EXTI3_IRQHandler(void)
{
  if (EXTI_GetITStatus(EXTI_Line3))
  {
    EXTI_ClearITPendingBit(EXTI_Line3);
    USART_SendData(USART6, 'f');
    USART_Send_str(USART2 , "Fire" , sizeof("Fire"));
  }
}

//光强传感器中断  模拟开门
void EXTI2_IRQHandler(void)
{
  if (EXTI_GetITStatus(EXTI_Line2))
  {
    EXTI_ClearITPendingBit(EXTI_Line2);
    USART_Send_str(USART2 , "go home" , sizeof("go home"));
  }
}

/**
  * @}
  */ 

/**
  * @}
  */ 


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
